﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VgoCardChange.Models;
using VgoCardChange.Models.Objects;

namespace VgoCardChange.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetNews()
        {
            NewsModels NewMd = new NewsModels();
            List<News> listnews = new List<News>();
            listnews = NewMd.GetNews();
            ViewData["listnews"] = listnews;
            return View();
        }
        public ActionResult CreateNews()
        {
            string date = DateTime.Today.ToShortDateString();
            ViewData["datenow"] = date;
            UserModels UsMd = new UserModels();
            CateModels CatMd = new CateModels();
            //ItemRelayModels IRelayMd = new ItemRelayModels();
            ViewData["listuser"] = new SelectList(UsMd.GetUser(), "UserID", "FullName");
            ViewData["listcate"] = new SelectList(CatMd.GetCates(), "CateID", "CateName");
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateNews(string NewsName, string Content, int listcate)
        {
            string date = DateTime.Today.ToShortDateString();
            ViewData["datenow"] = date;          
            NewsModels NewMd = new NewsModels();
            News news = new News();
            news.NewsName = NewsName;
            news.DatePost = DateTime.Parse(date);           
           // news.DatePost = DatePost;
            news.Content = Content;
            news.CateID = listcate;
            string username = Session["UserName"].ToString();
            news.UserID = NewMd.GetUserID(username);
            NewMd.CreateNew(news);

            return RedirectToAction("GetNews");
        }
        public ActionResult EditNews(int NewsID)
        {
            UserModels UsMd = new UserModels();
            CateModels CatMd = new CateModels();
            ViewData["listcate1"] = new SelectList(CatMd.GetCateName(),"CateID","CateName",NewsID);
            ViewData["listuser"] = new SelectList(UsMd.GetUser(), "UserID", "FullName", NewsID);
            string date = DateTime.Today.ToShortDateString();
            ViewData["datenow"] = date;
            NewsModels NewMd = new NewsModels();
            List<News> listnews = new List<News>();
            listnews = NewMd.GetNewsID(NewsID);
            ViewData["listnewsid"] = listnews;
            return View(listnews);
        
        
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditNews(int NewsID, string NewsName, string Content, int listcatename)
        {
            CateModels CatMd = new CateModels();
            ViewData["listcate1"] = new SelectList(CatMd.GetCateName(), "CateID", "CateName", NewsID);
            string date = DateTime.Today.ToShortDateString();
            ViewData["datenow"] = date;
            NewsModels NewMd = new NewsModels();
            News news = new News();
            news.NewsID = NewsID;
            news.NewsName = NewsName;
           // DatePost = DateTime.Parse(date);
            news.DatePost =DateTime.Parse (date);
            news.Content = Content;
           // news.CateID = listcatename;
            string username = Session["UserName"].ToString();
            news.UserID = NewMd.GetUserID(username);
            NewMd.EditNews(news);
            return RedirectToAction("GetNews");
        }
        public ActionResult DeleteNews(int NewsID)
        {
            NewsModels NewMd = new NewsModels();
            List<News> listnews = new List<News>();
            listnews = NewMd.GetNewsID(NewsID);
            ViewData["listnewsid"] = listnews;
            return View(listnews);

        }
             [HttpPost]
        public ActionResult DeleteNews(int NewsID, FormCollection coll)
        {
            NewsModels NewMd = new NewsModels();
            NewMd.DeleteNews(NewsID);
            return RedirectToAction("GetNews");
        }
             public ActionResult EditNew1(int NewsID)
             {
                 UserModels UsMd = new UserModels();
                 CateModels CatMd = new CateModels();
                 ViewData["listuser"] = new SelectList(UsMd.GetUser(), "UserID", "UserName", NewsID);
                 ViewData["listcate"] = new SelectList(CatMd.GetCates(), "CateID", "CateName", NewsID);
                 string date = DateTime.Today.ToShortDateString();
                 ViewData["datenow"] = date;
                 NewsModels NewMd = new NewsModels();
                 List<News> listnews = new List<News>();
                 listnews = NewMd.GetNewsID(NewsID);
                 ViewData["listnewsid"] = listnews;
                // return View(listnews);
                 return View();
             }
             [HttpPost]
             public ActionResult EditNew1(int NewsID, string NewsName, string Content, int listcate)
             {
                 CateModels CatMd = new CateModels();
                 ViewData["listcate1"] = new SelectList(CatMd.GetCateName(), "CateID", "CateName", NewsID);
                 string date = DateTime.Today.ToShortDateString();
                 ViewData["datenow"] = date;
                 NewsModels NewMd = new NewsModels();
                 News news = new News();
                 news.NewsID = NewsID;
                 news.NewsName = NewsName;
                 // DatePost = DateTime.Parse(date);
                 news.DatePost = DateTime.Parse(date);
                 news.Content = Content;
                 news.CateID = listcate;
                 string username = Session["UserName"].ToString();
                 news.UserID = NewMd.GetUserID(username);
                 NewMd.EditNews(news);
                 return RedirectToAction("GetNews");
             }


    }
}
